﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using FTBS.Models.Entities;
using System.Transactions;

namespace FTBS.Models.DAO
{
    public class PriceTicketDAO
    {
        private FilmTicketBookingSystemEntities _entities = new FilmTicketBookingSystemEntities();
        private TicketDAO _ticketDao = new TicketDAO();

        public double GetPriceTicket(int theaterId, int chairTypeId)
        {
            double price = 0;
            var priceTicket = from pt in _entities.PriceTickets
                              where pt.ChairTypeId == chairTypeId && pt.TheaterId == theaterId && pt.IsDeleted == false
                              select pt;
            if (priceTicket.FirstOrDefault() != null)
            {
                price = priceTicket.FirstOrDefault().Price;
            }
            return price;
        }

        public double GetSalePriceTicket(int theaterId, int chairTypeId)
        {
            double salePrice = 0;
            var priceTicket = from pt in _entities.PriceTickets
                              where pt.ChairTypeId == chairTypeId && pt.TheaterId == theaterId && pt.IsDeleted == false
                              select pt;
            if (priceTicket.FirstOrDefault() != null)
            {
                salePrice = priceTicket.FirstOrDefault().SalePrice;
            }
            return salePrice;
        }

        public int GetPriceTicketId(int theaterId, int chairTypeId)
        {
            int id = 0;
            var priceTicket = from pt in _entities.PriceTickets
                              where pt.ChairTypeId == chairTypeId && pt.TheaterId == theaterId && pt.IsDeleted == false
                              select pt;
            if (priceTicket.FirstOrDefault() != null)
            {
                id = priceTicket.FirstOrDefault().PriceTicketId;
            }
            return id;
        }

        public PriceTicket GetPriceTicketById(int id)
        {
            var priceTicket = _entities.PriceTickets.Find(id);
            return priceTicket;
        }

        public int Add(List<PriceTicket> lstPriceTicket)
        {
            try
            {
                using (TransactionScope trans = new TransactionScope())
                {
                    try
                    {
                        foreach (var priceTicket in lstPriceTicket)
                        {
                            _entities.PriceTickets.Add(priceTicket);
                        }
                        int result = _entities.SaveChanges();

                        trans.Complete();
                        return result;
                    }
                    catch (Exception)
                    {
                        trans.Dispose();
                        return 0;
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        //public int Add(PriceTicket priceTicket)
        //{
        //    try
        //    {
        //        _entities.PriceTickets.Add(priceTicket);
        //        int resultQuery = _entities.SaveChanges();
        //        return resultQuery;
        //    }
        //    catch (Exception ex)
        //    {
        //        throw ex;
        //    }
        //}

        public int Edit(List<PriceTicket> lstPriceTicket)
        {
            try
            {
                using (TransactionScope trans = new TransactionScope())
                {
                    try
                    {
                        foreach (var priceTicket in lstPriceTicket)
                        {
                            _ticketDao.UpdateTicketPurePrice(priceTicket);
                        }

                        foreach (var priceTicket in lstPriceTicket)
                        {
                            PriceTicket pt = _entities.PriceTickets.Find(priceTicket.PriceTicketId);
                            pt.TheaterId = priceTicket.TheaterId;
                            pt.ChairTypeId = priceTicket.ChairTypeId;
                            pt.SalePrice = priceTicket.SalePrice;
                            pt.Price = priceTicket.Price;
                            _entities.Entry(pt).State = EntityState.Modified;
                        }
                        int result = _entities.SaveChanges();
                        trans.Complete();
                        return result;
                    }
                    catch (Exception)
                    {
                        trans.Dispose();
                        return 0;
                    }
                }
            }
            catch (Exception ex)
            {
                return 0;
                //throw ex;
            }
        }

        public int EditBoughtPriceTickets(List<PriceTicket> lstPriceTicket)
        {
            try
            {
                using (TransactionScope trans = new TransactionScope())
                {
                    try
                    {
                        foreach (var priceTicket in lstPriceTicket)
                        {
                            PriceTicket pt = _entities.PriceTickets.Find(priceTicket.PriceTicketId);
                            pt.TheaterId = priceTicket.TheaterId;
                            pt.ChairTypeId = priceTicket.ChairTypeId;
                            pt.Price = priceTicket.Price;
                            pt.SalePrice = priceTicket.SalePrice;
                            _entities.Entry(pt).State = EntityState.Modified;
                        }
                        int result = _entities.SaveChanges();
                        trans.Complete();
                        return result;
                    }
                    catch (Exception)
                    {
                        trans.Dispose();
                        return 0;
                    }
                }
            }
            catch (Exception ex)
            {
                return 0;
                //throw ex;
            }
        }
        //public int Edit(int priceTicketId, double price)
        //{
        //    try
        //    {
        //        PriceTicket pt = _entities.PriceTickets.Find(priceTicketId);
        //        pt.Price = price;
        //        _entities.Entry(pt).State = EntityState.Modified;
        //        int resultQuery = _entities.SaveChanges();
        //        return resultQuery;
        //    }
        //    catch (Exception ex)
        //    {
        //        throw ex;
        //    }
        //}

        public int DeletePriceTicket(int priceTicketId)
        {
            try
            {
                PriceTicket pt = _entities.PriceTickets.Find(priceTicketId);
                pt.IsDeleted = true;
                _entities.Entry(pt).State = EntityState.Modified;
                int result = _entities.SaveChanges();
                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public int CheckIsExistedPriceTicket(int theaterId)
        {
            var numberOfPriceTickets =
                _entities.PriceTickets.Count(pt => pt.TheaterId == theaterId);
            return numberOfPriceTickets;
        }

        /// <summary>
        /// Check the ShowTimeId is used in Schedule or not
        /// This function is used for user Click to Delete in List ShowTIme
        /// </summary>
        /// <param name="showTimeId"></param>
        /// <returns>True: Is Exist ---- False: Not Exist</returns>
        /// <author>VuongVT</author>
        public bool IsPriceTicketUsedForSchedule(PriceTicket priceTicket)
        {
            try
            {
                var isUserForSchedule = _entities.Schedules.Any(schedule => schedule.TheaterId == priceTicket.TheaterId
                                                                            && schedule.IsDeleted == false
                                                                            && schedule.IsCreateTicket == true);
                return isUserForSchedule;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}